﻿using ODS.Config;
using ODS.Model;
using ODS.Utils;
using ODS.WebService.DeviceData;
using ODS.WebService.DeviceFunctions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ODS.ViewModel
{
    public class MainViewModel : INotifyPropertyChanged
    {

        public MainViewModel()
        {
            DeviceViewModelList = new ObservableCollection<DeviceViewModel>();
            TotalMessageViewModel = new TotalMessageViewModel();
            //ListMessagesOfCurrentDeviceViewModel = new TotalMessageViewModel();
        }

        ObservableCollection<DeviceViewModel> deviceViewModelList;

        public ObservableCollection<DeviceViewModel> DeviceViewModelList
        {
            get { return deviceViewModelList; }
            set { deviceViewModelList = value; }
        }

        TotalMessageViewModel totalMessageViewModel;

        public TotalMessageViewModel TotalMessageViewModel
        {
            get { return totalMessageViewModel; }
            set { totalMessageViewModel = value; }
        }

        //TotalMessageViewModel listMessagesOfCurrentDeviceViewModel;

        //public TotalMessageViewModel ListMessagesOfCurrentDeviceViewModel
        //{
        //    get { return listMessagesOfCurrentDeviceViewModel; }
        //    set { listMessagesOfCurrentDeviceViewModel = value; }
        //}


        public async Task LoadListDeviceFromUser()
        {

            InfoResult InfoResult_RetrievesDeviceDegists = await RetrievesDeviceDigestListService.RetrievesDeviceDigestList();

            if (InfoResult_RetrievesDeviceDegists.IsSuccess)
            {
                //only reload after call ws success
                deviceViewModelList.Clear();
                totalMessageViewModel.MessageViewModelList.Clear();
                totalMessageViewModel.TotalUnreadMessage = 0;

                List<DeviceModel> listDeviceModel = (List<DeviceModel>)InfoResult_RetrievesDeviceDegists.ReturnedObject;

                foreach (DeviceModel deviceModel in listDeviceModel)
                {
                    DeviceViewModel DeviceViewModel = new DeviceViewModel(deviceModel);
                    DeviceViewModel.TotalMessageViewModel = new TotalMessageViewModel();
                    DeviceViewModel.TotalMessageViewModel.MessageViewModelList = new ObservableCollection<MessageViewModel>();

                    if (!String.IsNullOrEmpty(deviceModel.UnreadNumber))
                    {
                        DeviceViewModel.TotalMessageViewModel.TotalUnreadMessage = int.Parse(deviceModel.UnreadNumber);
                        totalMessageViewModel.TotalUnreadMessage += DeviceViewModel.TotalMessageViewModel.TotalUnreadMessage;
                        
                    }

                    DeviceViewModelList.Add(DeviceViewModel);
                }


            }
            else
            {
                MessageBox.Show(InfoResult_RetrievesDeviceDegists.Error);
            }
             
        }

        public DeviceViewModel getDeviceViewModelFromDeviceIMEI(String deviceIMEI)
        {
            foreach (DeviceViewModel deviceVM in deviceViewModelList)
            {
                if (deviceVM.DeviceModel.DeviceIMEI.Equals(deviceIMEI))
                {
                    return deviceVM;
                }
            }
            return null;
        }

        public async Task loadListUnreadMessagesOfCurrentDevice(DeviceModel currentDeviceModel, int page, int offset)
        {
            DeviceViewModel deviceVM = getDeviceViewModelFromDeviceIMEI(currentDeviceModel.DeviceIMEI);

            String messError = null;

            InfoResult result_numberUnread = await LoadNumberUnreadEventService.LoadNumberUnreadEvent(currentDeviceModel.DeviceIMEI);
            if (result_numberUnread.IsSuccess)
            {
                if (result_numberUnread.ReturnedObject != null)
                {
                    deviceVM.TotalMessageViewModel.TotalUnreadMessage = int.Parse(result_numberUnread.ReturnedObject as String);
                }

            }
            else
            {
                messError = result_numberUnread.Error;
            }

            InfoResult result = await LoadDeviceUnreadEventsService.LoadDeviceUnreadEvents(currentDeviceModel.DeviceIMEI, page, offset);
            if (result.IsSuccess)
            {
                if (result.ReturnedObject != null)
                {
                    if (offset == 0)
                        deviceVM.TotalMessageViewModel.MessageViewModelList.Clear();

                    List<EventDetailModel> eventDetailModels = result.ReturnedObject as List<EventDetailModel>;
                    foreach (EventDetailModel eventDetail in eventDetailModels)
                    {
                        eventDetail.DeviceIMEI = currentDeviceModel.DeviceIMEI;
                        MessageViewModel messageVM = new MessageViewModel(eventDetail);
                        deviceVM.TotalMessageViewModel.MessageViewModelList.Add(messageVM);

                    }
                }
            }
            else
            {
                messError = result.Error;
            }

            if (messError != null)
                MessageBox.Show(messError);


        }

        public async void getNumberUnreadMessage(String deviceIMEI)
        {
            DeviceViewModel deviceVM = getDeviceViewModelFromDeviceIMEI(deviceIMEI);
            String messError = null;

            InfoResult result_numberUnread = await LoadNumberUnreadEventService.LoadNumberUnreadEvent(deviceIMEI);
            if (result_numberUnread.IsSuccess)
            {
                if (result_numberUnread.ReturnedObject != null)
                {
                    deviceVM.TotalMessageViewModel.TotalUnreadMessage = int.Parse(result_numberUnread.ReturnedObject as String);
                }

            }
            else
            {
                messError = result_numberUnread.Error;
            }

            if (messError != null)
                MessageBox.Show(messError);
        }
       

        public async Task loadListMessagesOfCurrentDevice(DeviceModel currentDeviceModel, int page, int offset)
        {
            DeviceViewModel deviceVM = getDeviceViewModelFromDeviceIMEI(currentDeviceModel.DeviceIMEI);

            String messError = null;

            InfoResult result_numberUnread = await LoadNumberUnreadEventService.LoadNumberUnreadEvent(currentDeviceModel.DeviceIMEI);
            if (result_numberUnread.IsSuccess)
            {
                if (result_numberUnread.ReturnedObject != null)
                {
                    deviceVM.TotalMessageViewModel.TotalUnreadMessage = int.Parse(result_numberUnread.ReturnedObject as String);
                }

            }
            else
            {
                messError = result_numberUnread.Error;
            }

            DateTime minDate = CommonUtil.ParseDateTimeFromString("1990-01-01T12:00:00.001Z");
            DateTime maxDate = DateTime.Now;
            InfoResult result = await FindDeviceEventsSevice.FindDeviceEvent(currentDeviceModel.DeviceIMEI, minDate, maxDate, page, offset);

            if (result.IsSuccess)
            {
                if (result.ReturnedObject != null)
                {
                    if (offset == 0)
                        deviceVM.TotalMessageViewModel.MessageViewModelList.Clear();

                    List<EventDetailModel> listEvents = result.ReturnedObject as List<EventDetailModel>;
                    foreach (EventDetailModel eventDetail in listEvents)
                    {
                        eventDetail.DeviceIMEI = currentDeviceModel.DeviceIMEI;
                        MessageViewModel messageVM = new MessageViewModel(eventDetail);
                        deviceVM.TotalMessageViewModel.MessageViewModelList.Add(messageVM);

                    }
                }
            }
            else
            {
               messError = result.Error;
            }

            if (messError != null)
                MessageBox.Show(messError);

            

        }

        public async Task LoadAllMessagesOfUser()
        {
            TotalMessageViewModel.MessageViewModelList.Clear();

            String message = null;


            DateTime minDate = DateTime.MinValue;
            DateTime maxDate = DateTime.Now;
            foreach (DeviceViewModel deviceModel in deviceViewModelList)
            {
                InfoResult result = await FindDeviceEventsSevice.FindDeviceEvent(deviceModel.DeviceModel.DeviceIMEI, minDate, maxDate, 200, 0);

                if (result.IsSuccess)
                {
                    if (result.ReturnedObject != null)
                    {

                        List<EventDetailModel> listEvents = result.ReturnedObject as List<EventDetailModel>;
                        foreach (EventDetailModel eventDetail in listEvents)
                        {
                            eventDetail.DeviceIMEI = deviceModel.DeviceModel.DeviceIMEI;
                            MessageViewModel messageVM = new MessageViewModel(eventDetail);
                            TotalMessageViewModel.MessageViewModelList.Add(messageVM);

                        }

                    }
                }
                else
                {
                    message = result.Error;
                }
            }

            if (message != null)
                MessageBox.Show(message);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}
